/*      */ package com.zimbra.cs.zimlet;
/*      */ 
/*      */ import com.zimbra.common.account.Key.CosBy;
/*      */ import com.zimbra.common.auth.ZAuthToken;
/*      */ import com.zimbra.common.httpclient.HttpClientUtil;
/*      */ import com.zimbra.common.localconfig.DebugConfig;
/*      */ import com.zimbra.common.localconfig.KnownKey;
/*      */ import com.zimbra.common.localconfig.LC;
/*      */ import com.zimbra.common.service.ServiceException;
/*      */ import com.zimbra.common.soap.AdminConstants;
/*      */ import com.zimbra.common.soap.Element;
/*      */ import com.zimbra.common.soap.Element.XMLElement;
/*      */ import com.zimbra.common.soap.SoapHttpTransport;
/*      */ import com.zimbra.common.soap.SoapTransport;
/*      */ import com.zimbra.common.soap.W3cDomUtil;
/*      */ import com.zimbra.common.util.ByteUtil;
/*      */ import com.zimbra.common.util.CliUtil;
/*      */ import com.zimbra.common.util.FileUtil;
/*      */ import com.zimbra.common.util.Log;
/*      */ import com.zimbra.common.util.Pair;
/*      */ import com.zimbra.common.util.StringUtil;
/*      */ import com.zimbra.common.util.ZimbraLog;
/*      */ import com.zimbra.cs.account.Account;
/*      */ import com.zimbra.cs.account.AccountServiceException;
/*      */ import com.zimbra.cs.account.Cos;
/*      */ import com.zimbra.cs.account.Domain;
/*      */ import com.zimbra.cs.account.Provisioning;
/*      */ import com.zimbra.cs.account.Server;
/*      */ import com.zimbra.cs.account.Zimlet;
/*      */ import com.zimbra.cs.account.soap.SoapProvisioning;
/*      */ import com.zimbra.cs.httpclient.URLUtil;
/*      */ import com.zimbra.cs.service.admin.FlushCache;
/*      */ import com.zimbra.cs.util.BuildInfo;
/*      */ import com.zimbra.cs.util.WebClientServiceUtil;
/*      */ import com.zimbra.soap.admin.type.CacheEntryType;
/*      */ import java.io.ByteArrayInputStream;
/*      */ import java.io.File;
/*      */ import java.io.FileInputStream;
/*      */ import java.io.FileOutputStream;
/*      */ import java.io.IOException;
/*      */ import java.io.OutputStream;
/*      */ import java.io.PrintStream;
/*      */ import java.net.FileNameMap;
/*      */ import java.net.URL;
/*      */ import java.net.URLClassLoader;
/*      */ import java.net.URLConnection;
/*      */ import java.util.ArrayList;
/*      */ import java.util.Arrays;
/*      */ import java.util.Collection;
/*      */ import java.util.Collections;
/*      */ import java.util.Comparator;
/*      */ import java.util.HashMap;
/*      */ import java.util.HashSet;
/*      */ import java.util.List;
/*      */ import java.util.Map;
/*      */ import java.util.Map.Entry;
/*      */ import java.util.Set;
/*      */ import java.util.concurrent.TimeUnit;
/*      */ import java.util.jar.JarOutputStream;
/*      */ import java.util.jar.Manifest;
/*      */ import java.util.regex.Matcher;
/*      */ import java.util.regex.Pattern;
/*      */ import java.util.zip.CRC32;
/*      */ import java.util.zip.ZipEntry;
/*      */ import java.util.zip.ZipOutputStream;
/*      */ import org.apache.commons.httpclient.Cookie;
/*      */ import org.apache.commons.httpclient.HttpClient;
/*      */ import org.apache.commons.httpclient.HttpState;
/*      */ import org.apache.commons.httpclient.methods.PostMethod;
/*      */ import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
/*      */ import org.apache.commons.httpclient.methods.multipart.Part;
/*      */ import org.apache.commons.httpclient.methods.multipart.PartBase;
/*      */ import org.apache.commons.httpclient.params.HttpClientParams;
/*      */ import org.apache.commons.httpclient.params.HttpMethodParams;
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ public class ZimletUtil
/*      */ {
/*      */   public static final String ZIMLET_BASE = "/service/zimlet";
/*      */   public static final String ZIMLET_DEV_DIR = "_dev";
/*      */   public static final String ZIMLET_ALLOWED_DOMAINS = "allowedDomains";
/*      */   public static final String ZIMLET_DEFAULT_COS = "default";
/*      */   public static final String PARAM_ZIMLET = "Zimlet";
/*      */   public static final String ZIMLET_NAME_REGEX = "^[\\w.-]+$";
/*      */   private static final String ZIMLET_CACHE_DIR = "/opt/zimbra/jetty/work/resource-cache/zimletres/latest";
/*  105 */   private static int P_MAX = Integer.MAX_VALUE;
/*  106 */   private static boolean sZimletsLoaded = false;
/*  107 */   private static Map<String, ZimletFile> sZimlets = new HashMap();
/*      */   
/*  109 */   private static Map<String, Class> sZimletHandlers = new HashMap();
/*      */   private static final int INSTALL_ZIMLET = 10;
/*      */   
/*  112 */   public static void migrateUserPrefIfNecessary(Account acct) throws ServiceException { if (!DebugConfig.enableMigrateUserZimletPrefs) {
/*  113 */       return;
/*      */     }
/*      */     
/*  116 */     Set<String> wanted = acct.getMultiAttrSet("zimbraPrefZimlets");
/*  117 */     Set<String> unwanted = acct.getMultiAttrSet("zimbraPrefDisabledZimlets");
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  122 */     boolean needsMigrate = (!wanted.isEmpty()) && (unwanted.isEmpty());
/*      */     
/*  124 */     if (!needsMigrate) {
/*  125 */       return;
/*      */     }
/*      */     
/*  128 */     ZimletPresence availZimlets = getAvailableZimlets(acct);
/*  129 */     Map<String, Object> attrs = new HashMap();
/*  130 */     StringBuilder disabledZimletNamesForLogging = new StringBuilder();
/*      */     
/*  132 */     for (String zimletName : availZimlets.getZimletNames()) {
/*  133 */       ZimletPresence.Presence presence = availZimlets.getPresence(zimletName);
/*      */       
/*  135 */       if ((presence == ZimletPresence.Presence.enabled) && (!wanted.contains(zimletName))) {
/*  136 */         disabledZimletNamesForLogging.append(zimletName + ", ");
/*  137 */         StringUtil.addToMultiMap(attrs, "zimbraPrefDisabledZimlets", zimletName);
/*      */       }
/*      */     }
/*      */     
/*      */ 
/*      */ 
/*  143 */     if (attrs.isEmpty()) {
/*  144 */       ZimbraLog.account.info("Not migrating zimbraPrefDisabledZimlets for account " + acct.getName() + " because all zimlets are enabled");
/*      */       
/*  146 */       return;
/*      */     }
/*      */     
/*  149 */     ZimbraLog.account.info("Migrating zimbraPrefDisabledZimlets for account " + acct.getName() + " to " + disabledZimletNamesForLogging.toString());
/*      */     
/*      */ 
/*  152 */     Provisioning prov = Provisioning.getInstance();
/*  153 */     prov.modifyAttrs(acct, attrs);
/*      */   }
/*      */   
/*      */   public static ZimletPresence getUserZimlets(Account acct) throws ServiceException {
/*  157 */     ZimletPresence userZimlets = getAvailableZimlets(acct);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  163 */     String[] userPrefEnabledZimlets = acct.getMultiAttr("zimbraPrefZimlets");
/*  164 */     for (String zimletName : userPrefEnabledZimlets) {
/*  165 */       ZimletPresence.Presence presence = userZimlets.getPresence(zimletName);
/*  166 */       if ((presence != null) && (presence != ZimletPresence.Presence.mandatory) && (presence != ZimletPresence.Presence.enabled))
/*      */       {
/*      */ 
/*  169 */         userZimlets.put(zimletName, ZimletPresence.Presence.enabled);
/*      */       }
/*      */     }
/*      */     
/*  173 */     String[] userPrefDisabledZimlets = acct.getMultiAttr("zimbraPrefDisabledZimlets");
/*  174 */     for (String zimletName : userPrefDisabledZimlets) {
/*  175 */       ZimletPresence.Presence presence = userZimlets.getPresence(zimletName);
/*  176 */       if ((presence != null) && (presence != ZimletPresence.Presence.mandatory) && (presence != ZimletPresence.Presence.disabled))
/*      */       {
/*      */ 
/*  179 */         userZimlets.put(zimletName, ZimletPresence.Presence.disabled);
/*      */       }
/*      */     }
/*      */     
/*  183 */     return userZimlets;
/*      */   }
/*      */   
/*      */   public static ZimletPresence getAvailableZimlets(Account acct) throws ServiceException {
/*  187 */     ZimletPresence availZimlets = new ZimletPresence();
/*      */     
/*      */ 
/*  190 */     Domain domain = Provisioning.getInstance().getDomain(acct);
/*  191 */     if (domain != null) {
/*  192 */       String[] domainZimlets = domain.getMultiAttr("zimbraZimletDomainAvailableZimlets");
/*  193 */       for (String zimletWithPrefix : domainZimlets) {
/*  194 */         availZimlets.put(zimletWithPrefix);
/*      */       }
/*      */     }
/*      */     
/*  198 */     String[] acctCosZimlets = acct.getMultiAttr("zimbraZimletAvailableZimlets");
/*  199 */     for (String zimletWithPrefix : acctCosZimlets) {
/*  200 */       availZimlets.put(zimletWithPrefix);
/*      */     }
/*      */     
/*  203 */     return availZimlets;
/*      */   }
/*      */   
/*      */   public static ZimletPresence getAvailableZimlets(Cos cos) throws ServiceException {
/*  207 */     ZimletPresence availZimlets = new ZimletPresence();
/*      */     
/*  209 */     String[] acctCosZimlets = cos.getMultiAttr("zimbraZimletAvailableZimlets");
/*  210 */     for (String zimletWithPrefix : acctCosZimlets) {
/*  211 */       availZimlets.put(zimletWithPrefix);
/*      */     }
/*      */     
/*  214 */     return availZimlets;
/*      */   }
/*      */   
/*      */   public static String[] listZimletNames() {
/*  218 */     String[] zimlets = (String[])sZimlets.keySet().toArray(new String[0]);
/*  219 */     Arrays.sort(zimlets);
/*  220 */     return zimlets;
/*      */   }
/*      */   
/*      */   public static String[] listDevZimletNames() {
/*  224 */     String[] zimlets = (String[])loadDevZimlets().keySet().toArray(new String[0]);
/*  225 */     Arrays.sort(zimlets);
/*  226 */     return zimlets;
/*      */   }
/*      */   
/*      */   private static final int UNINSTALL_ZIMLET = 11;
/*      */   public static List<Zimlet> orderZimletsByPriority(List<Zimlet> zimlets)
/*      */   {
/*  232 */     List<Pair<Version, Zimlet>> plist = new ArrayList();
/*  233 */     for (Zimlet z : zimlets) {
/*  234 */       String pstring = z.getPriority();
/*  235 */       if (pstring == null)
/*      */       {
/*  237 */         pstring = Integer.toString(Integer.MAX_VALUE);
/*      */       }
/*  239 */       Version v = new Version(pstring);
/*  240 */       plist.add(new Pair(v, z));
/*      */     }
/*  242 */     Collections.sort(plist, new Comparator()
/*      */     {
/*      */       public int compare(Pair<Version, Zimlet> first, Pair<Version, Zimlet> second)
/*      */       {
/*  246 */         return ((Version)first.getFirst()).compareTo((Version)second.getFirst());
/*      */       }
/*      */       
/*  249 */     });
/*  250 */     List<Zimlet> ret = new ArrayList();
/*  251 */     for (Pair<Version, Zimlet> p : plist) {
/*  252 */       ret.add(p.getSecond());
/*      */     }
/*  254 */     return ret;
/*      */   }
/*      */   
/*      */   public static List<Zimlet> orderZimletsByPriority(String[] zimlets) {
/*  258 */     Provisioning prov = Provisioning.getInstance();
/*  259 */     List<Zimlet> zlist = new ArrayList();
/*  260 */     for (int i = 0; i < zimlets.length; i++) {
/*      */       try {
/*  262 */         Zimlet z = prov.getZimlet(zimlets[i]);
/*  263 */         if (z != null) {
/*  264 */           zlist.add(z);
/*      */         }
/*      */       }
/*      */       catch (ServiceException se) {
/*  268 */         ZimbraLog.zimlet.warn("unable to get zimlet " + zimlets[i], se);
/*      */       }
/*      */     }
/*  271 */     return orderZimletsByPriority(zlist);
/*      */   }
/*      */   
/*      */   public static List<Zimlet> orderZimletsByPriority() throws ServiceException {
/*  275 */     Provisioning prov = Provisioning.getInstance();
/*  276 */     List<Zimlet> allzimlets = prov.listAllZimlets();
/*  277 */     return orderZimletsByPriority(allzimlets);
/*      */   }
/*      */   
/*      */   public static void updateZimletConfig(String zimlet, String config) throws ServiceException {
/*  281 */     Provisioning prov = Provisioning.getInstance();
/*  282 */     Zimlet zim = prov.getZimlet(zimlet);
/*  283 */     if (zim == null) {
/*  284 */       throw AccountServiceException.NO_SUCH_ZIMLET(zimlet);
/*      */     }
/*  286 */     Map<String, String> map = new HashMap();
/*  287 */     map.put("zimbraZimletHandlerConfig", config);
/*  288 */     prov.modifyAttrs(zim, map);
/*      */   }
/*      */   
/*      */   public static ZimletConfig getZimletConfig(String zimlet) throws ServiceException {
/*  292 */     Provisioning prov = Provisioning.getInstance();
/*  293 */     Zimlet z = prov.getZimlet(zimlet);
/*  294 */     if (z == null) {
/*  295 */       throw AccountServiceException.NO_SUCH_ZIMLET(zimlet);
/*      */     }
/*  297 */     String cf = z.getAttr("zimbraZimletHandlerConfig");
/*  298 */     if (cf == null) {
/*  299 */       return null;
/*      */     }
/*      */     try {
/*  302 */       return new ZimletConfig(cf);
/*      */     } catch (ZimletException e) {
/*  304 */       ZimbraLog.zimlet.warn("Unable to load zimlet config for " + z.getName(), e);
/*      */     }
/*  306 */     return null;
/*      */   }
/*      */   
/*      */   private static final int LIST_ZIMLETS = 12;
/*      */   private static final int ACL_ZIMLET = 13;
/*      */   private static final int LIST_ACLS = 14;
/*      */   private static final int DUMP_CONFIG = 15;
/*      */   private static final int INSTALL_CONFIG = 16;
/*      */   private static final int LDAP_DEPLOY = 17;
/*      */   public static ZimletHandler getHandler(String name)
/*      */   {
/*  317 */     loadZimlets();
/*  318 */     Class zh = (Class)sZimletHandlers.get(name);
/*  319 */     if (zh == null) {
/*  320 */       ZimletFile zf = (ZimletFile)sZimlets.get(name);
/*  321 */       if (zf == null) {
/*  322 */         return null;
/*      */       }
/*  324 */       URLClassLoader cl = null;
/*      */       try {
/*  326 */         String clazz = zf.getZimletDescription().getServerExtensionClass();
/*  327 */         URL[] urls; if (clazz != null) {
/*  328 */           urls = new URL[] { zf.toURL() };
/*  329 */           cl = new URLClassLoader(urls, ZimletUtil.class.getClassLoader());
/*  330 */           zh = cl.loadClass(clazz);
/*  331 */           ZimbraLog.zimlet.info("Loaded class " + zh.getName());
/*  332 */           sZimletHandlers.put(name, zh);
/*      */         }
/*      */         
/*      */ 
/*      */ 
/*      */ 
/*  338 */         if (cl != null) {
/*      */           try {
/*  340 */             cl.close();
/*      */           } catch (IOException e) {
/*  342 */             ZimbraLog.zimlet.warn("failed to close URLClassLoader", e);
/*      */           }
/*      */         }
/*      */         
/*      */         try
/*      */         {
/*  348 */           if (zh != null) {
/*  349 */             return (ZimletHandler)zh.newInstance();
/*      */           }
/*      */         } catch (Exception e) {
/*  352 */           ZimbraLog.zimlet.warn("Unable to instantiate zimlet handler for " + name, e);
/*      */         }
/*      */       }
/*      */       catch (Exception e)
/*      */       {
/*  335 */         ZimbraLog.zimlet.warn("Unable to load zimlet handler for %s", name, e);
/*  336 */         return null;
/*      */       } finally {
/*  338 */         if (cl != null) {
/*      */           try {
/*  340 */             cl.close();
/*      */           } catch (IOException e) {
/*  342 */             ZimbraLog.zimlet.warn("failed to close URLClassLoader", e);
/*      */           }
/*      */         }
/*      */       }
/*      */     }
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  354 */     return null;
/*      */   }
/*      */   
/*      */   public static ZimletFile getZimlet(String zimlet) {
/*  358 */     ZimletFile zf = null;
/*  359 */     loadZimlets();
/*  360 */     zf = (ZimletFile)sZimlets.get(zimlet);
/*  361 */     if (zf != null) {
/*  362 */       return zf;
/*      */     }
/*  364 */     return (ZimletFile)loadDevZimlets().get(zimlet);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static synchronized Map<String, ZimletFile> loadZimlets()
/*      */   {
/*  373 */     if (!sZimletsLoaded) {
/*  374 */       loadZimletsFromDir(sZimlets, LC.zimlet_directory.value());
/*  375 */       sZimletsLoaded = true;
/*      */     }
/*      */     
/*  378 */     return sZimlets;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static Map<String, ZimletFile> loadDevZimlets()
/*      */   {
/*  387 */     Map<String, ZimletFile> zimletMap = new HashMap();
/*  388 */     loadZimletsFromDir(zimletMap, LC.zimlet_directory.value() + File.separator + "_dev");
/*  389 */     return zimletMap;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */   public static void reloadZimlet(String zimlet)
/*      */     throws ZimletException
/*      */   {
/*      */     ZimletFile zf;
/*      */     
/*      */ 
/*      */     try
/*      */     {
/*  402 */       zf = new ZimletFile(LC.zimlet_directory.value() + File.separator + zimlet);
/*      */     } catch (IOException ioe) {
/*  404 */       ZimbraLog.zimlet.warn(ioe.getMessage());
/*  405 */       return;
/*      */     }
/*  407 */     synchronized (sZimlets) {
/*  408 */       sZimlets.remove(zimlet);
/*  409 */       sZimlets.put(zimlet, zf);
/*      */     }
/*      */   }
/*      */   
/*      */   private static final int DEPLOY_ZIMLET = 18;
/*      */   private static final int ENABLE_ZIMLET = 19;
/*      */   private static final int DISABLE_ZIMLET = 20;
/*      */   private static final int LIST_PRIORITY = 21;
/*      */   private static final int SET_PRIORITY = 22;
/*      */   private static final int INFO = 23;
/*      */   private static final int CREATE_ZIP = 24;
/*      */   private static final int TEST = 99;
/*      */   private static final String INSTALL_CMD = "install";
/*      */   private static void loadZimletsFromDir(Map<String, ZimletFile> zimlets, String dir) {
/*  423 */     File zimletRootDir = new File(dir);
/*  424 */     if ((zimletRootDir == null) || (!zimletRootDir.exists()) || (!zimletRootDir.isDirectory())) {
/*  425 */       return;
/*      */     }
/*      */     
/*  428 */     ZimbraLog.zimlet.debug("Loading zimlets from " + zimletRootDir.getPath());
/*      */     
/*  430 */     synchronized (zimlets) {
/*  431 */       zimlets.clear();
/*  432 */       String[] zimletNames = zimletRootDir.list();
/*  433 */       assert (zimletNames != null);
/*  434 */       for (int i = 0; i < zimletNames.length; i++) {
/*  435 */         if (!zimletNames[i].equals("_dev"))
/*      */         {
/*      */           try
/*      */           {
/*  439 */             zimlets.put(zimletNames[i], new ZimletFile(zimletRootDir, zimletNames[i]));
/*      */           } catch (Exception e) {
/*  441 */             ZimbraLog.zimlet.warn("error loading zimlet " + zimletNames[i], e);
/*      */           }
/*      */         }
/*      */       }
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void listZimlet(Element elem, Zimlet zimlet, int priority, ZimletPresence.Presence presence)
/*      */   {
/*  456 */     loadZimlets();
/*  457 */     ZimletFile zf = (ZimletFile)sZimlets.get(zimlet.getName());
/*  458 */     if (zf == null) {
/*  459 */       ZimbraLog.zimlet.warn("cannot find zimlet " + zimlet.getName());
/*  460 */       return;
/*      */     }
/*  462 */     String zimletBase = "/service/zimlet/" + zimlet.getName() + "/";
/*  463 */     Element entry = elem.addElement("zimlet");
/*  464 */     Element zimletContext = entry.addElement("zimletContext");
/*  465 */     zimletContext.addAttribute("baseUrl", zimletBase);
/*  466 */     if (priority >= 0) {
/*  467 */       zimletContext.addAttribute("priority", priority);
/*      */     }
/*      */     
/*  470 */     if (presence == null) {
/*  471 */       presence = ZimletPresence.Presence.enabled;
/*      */     }
/*  473 */     zimletContext.addAttribute("presence", presence.toString());
/*      */     try
/*      */     {
/*  476 */       zf.getZimletDescription().addToElement(entry);
/*  477 */       String config = zimlet.getHandlerConfig();
/*  478 */       if (config != null) {
/*  479 */         entry.addElement(W3cDomUtil.parseXML(config, elem.getFactory()));
/*      */       }
/*      */     } catch (Exception e) {
/*  482 */       ZimbraLog.zimlet.warn("error loading zimlet " + zimlet, e);
/*      */     }
/*      */   }
/*      */   
/*      */   public static abstract interface DeployListener
/*      */   {
/*      */     public abstract void markFinished(Server paramServer);
/*      */     
/*      */     public abstract void markFailed(Server paramServer, Exception paramException);
/*      */   }
/*      */   
/*      */   public static void listDevZimlets(Element elem) {
/*  494 */     for (ZimletFile zim : loadDevZimlets().values()) {
/*  495 */       String zimletBase = "/service/zimlet/_dev/" + zim.getZimletName() + "/";
/*  496 */       Element entry = elem.addElement("zimlet");
/*  497 */       Element zimletContext = entry.addElement("zimletContext");
/*  498 */       zimletContext.addAttribute("baseUrl", zimletBase);
/*      */       
/*      */ 
/*  501 */       zimletContext.addAttribute("presence", ZimletPresence.Presence.enabled.toString());
/*      */       try
/*      */       {
/*  504 */         zim.getZimletDescription().addToElement(entry);
/*  505 */         if (zim.hasZimletConfig()) {
/*  506 */           zim.getZimletConfig().addToElement(entry);
/*      */         }
/*      */       } catch (Exception e) {
/*  509 */         ZimbraLog.zimlet.warn("error loading dev zimlet: " + zim.getName(), e);
/*      */       }
/*      */     }
/*      */   }
/*      */   
/*      */   private static Map<String, Object> descToMap(ZimletDescription zd) throws ZimletException {
/*  515 */     Map<String, Object> attrs = new HashMap();
/*  516 */     attrs.put("zimbraZimletKeyword", zd.getServerExtensionKeyword());
/*  517 */     attrs.put("zimbraZimletVersion", zd.getVersion().toString());
/*  518 */     attrs.put("zimbraZimletDescription", zd.getDescription());
/*  519 */     attrs.put("zimbraZimletHandlerClass", zd.getServerExtensionClass());
/*  520 */     attrs.put("zimbraZimletServerIndexRegex", zd.getRegexString());
/*      */     
/*      */ 
/*      */ 
/*  524 */     return attrs;
/*      */   }
/*      */   
/*      */   public static File getZimletRootDir(String zimletName) throws ZimletException {
/*  528 */     if (zimletName.matches("^[\\w.-]+$")) {
/*  529 */       return new File(LC.zimlet_directory.value(), zimletName);
/*      */     }
/*  531 */     throw ZimletException.INVALID_ZIMLET_NAME();
/*      */   }
/*      */   
/*      */   public static void flushCache() throws ZimletException
/*      */   {
/*  536 */     sZimletsLoaded = false;
/*      */     try {
/*  538 */       Provisioning.getInstance().flushCache(CacheEntryType.zimlet, null);
/*      */     } catch (ServiceException e) {
/*  540 */       throw ZimletException.CANNOT_FLUSH_CACHE(e);
/*      */     }
/*      */   }
/*      */   
/*      */   public static void flushDiskCache(Map<String, Object> context) throws ServiceException {
/*  545 */     if (WebClientServiceUtil.isServerInSplitMode()) {
/*  546 */       List<Server> allServers = Provisioning.getInstance().getAllServers();
/*  547 */       for (Server server : allServers) {
/*  548 */         if (server.hasMailClientService()) {
/*  549 */           FlushCache.flushAllZimlets(context);
/*      */         } else {
/*  551 */           WebClientServiceUtil.sendFlushZimletRequestToUiNode(server);
/*      */         }
/*      */       }
/*      */     }
/*      */   }
/*      */   
/*      */   public static void flushAllZimletsCache() {
/*      */     try {
/*  559 */       File file = new File("/opt/zimbra/jetty/work/resource-cache/zimletres/latest");
/*  560 */       FileUtil.deleteDirContents(file);
/*      */     } catch (IOException e) {
/*  562 */       ZimbraLog.zimlet.warn("failed to flush zimlet cache", e);
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void deployZimletLocally(ZimletFile zf, DeployListener listener)
/*      */     throws IOException, ZimletException, ServiceException
/*      */   {
/*  573 */     Server localServer = Provisioning.getInstance().getLocalServer();
/*  574 */     deployZimletLocally(zf);
/*  575 */     if (listener != null) {
/*  576 */       listener.markFinished(localServer);
/*      */     }
/*      */   }
/*      */   
/*      */   static enum Action {
/*  581 */     INSTALL,  UPGRADE,  REPAIR;
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */     private Action() {}
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void deployZimletLocally(ZimletFile zf)
/*      */     throws IOException, ZimletException, ServiceException
/*      */   {
/*  598 */     Provisioning prov = Provisioning.getInstance();
/*  599 */     String zimletName = zf.getZimletName();
/*  600 */     ZimletDescription zd = zf.getZimletDescription();
/*      */     
/*  602 */     Action action = Action.INSTALL;
/*  603 */     String priority = null;
/*  604 */     boolean enable = true;
/*      */     
/*      */ 
/*  607 */     Zimlet z = prov.getZimlet(zimletName);
/*      */     
/*  609 */     if (z != null) {
/*  610 */       Version ver = new Version(z.getAttr("zimbraZimletVersion"));
/*  611 */       if (zd.getVersion().compareTo(ver) < 0) {
/*  612 */         ZimbraLog.zimlet.info("Zimlet " + zimletName + " being installed is of an older version.");
/*      */       }
/*  614 */       if (zd.getVersion().compareTo(ver) == 0) {
/*  615 */         action = Action.REPAIR;
/*      */       } else {
/*  617 */         action = Action.UPGRADE;
/*      */       }
/*      */       
/*  620 */       priority = z.getPriority();
/*  621 */       enable = z.isEnabled();
/*      */     }
/*      */     
/*      */ 
/*  625 */     z = ldapDeploy(zf);
/*      */     
/*      */ 
/*  628 */     installZimletLocally(zf);
/*      */     
/*  630 */     if (action == Action.REPAIR) {
/*  631 */       return;
/*      */     }
/*      */     
/*      */ 
/*  635 */     ZimbraLog.zimlet.info("Upgrading Zimlet " + zimletName + " to " + zd.getVersion().toString());
/*      */     
/*      */ 
/*  638 */     if (priority == null) {
/*  639 */       setPriority(zimletName, P_MAX);
/*      */     }
/*      */     
/*      */ 
/*  643 */     if (zf.hasZimletConfig()) {
/*  644 */       installConfig(zf.getZimletConfig());
/*      */     }
/*      */     
/*      */ 
/*  648 */     if (!zd.isExtension()) {
/*  649 */       activateZimlet(zimletName, "default");
/*      */     }
/*      */     
/*  652 */     if (!enable)
/*      */     {
/*  654 */       return;
/*      */     }
/*      */     
/*      */ 
/*  658 */     enableZimlet(zimletName);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void installZimletLocally(ZimletFile zf)
/*      */     throws IOException, ZimletException
/*      */   {
/*  671 */     ZimletDescription zd = zf.getZimletDescription();
/*  672 */     String zimletName = zd.getName();
/*  673 */     ZimbraLog.zimlet.info("Installing Zimlet " + zimletName + " on this host.");
/*      */     
/*      */ 
/*  676 */     File libDir = new File(LC.mailboxd_directory.value() + File.separator + "webapps" + File.separator + "zimlet" + File.separator + "WEB-INF" + File.separator + "lib");
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  682 */     File zimlet = getZimletRootDir(zimletName);
/*      */     
/*  684 */     zimlet.getParentFile().mkdirs();
/*  685 */     if (zimlet.exists()) {
/*  686 */       deleteFile(zimlet);
/*      */     }
/*      */     
/*  689 */     for (ZimletFile.ZimletEntry entry : zf.getAllEntries()) {
/*  690 */       String fname = entry.getName();
/*      */       File file;
/*  692 */       if (fname.endsWith(".jar")) {
/*  693 */         File file = new File(libDir, fname);
/*  694 */         if (!file.getCanonicalPath().startsWith(libDir.getCanonicalPath())) {
/*  695 */           ZimbraLog.zimlet.error(String.format("Zimlet %s has an invalid file path %s", new Object[] { zimletName, fname }));
/*  696 */           throw ZimletException.CANNOT_DEPLOY(zimletName, "Invalid file path " + fname, null);
/*      */         }
/*      */       } else {
/*  699 */         file = new File(zimlet, fname);
/*  700 */         if (!file.getCanonicalPath().startsWith(LC.zimlet_directory.value())) {
/*  701 */           ZimbraLog.zimlet.error(String.format("Zimlet %s has an invalid file path %s", new Object[] { zimletName, fname }));
/*  702 */           throw ZimletException.CANNOT_DEPLOY(zimletName, "Invalid file path " + fname, null);
/*      */         }
/*      */       }
/*  705 */       file.getParentFile().mkdirs();
/*  706 */       writeFile(entry.getContents(), file);
/*      */     }
/*      */     
/*  709 */     flushCache();
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void ldapDeploy(String zimlet)
/*      */     throws ServiceException, IOException, ZimletException
/*      */   {
/*  721 */     String zimletRoot = LC.zimlet_directory.value();
/*  722 */     ZimletFile zf = new ZimletFile(zimletRoot + File.separator + zimlet);
/*  723 */     ldapDeploy(zf);
/*      */   }
/*      */   
/*      */   public static Zimlet ldapDeploy(ZimletFile zf) throws ServiceException, IOException, ZimletException {
/*  727 */     ZimletDescription zd = zf.getZimletDescription();
/*  728 */     String zimletName = zd.getName();
/*  729 */     Map<String, Object> attrs = descToMap(zd);
/*  730 */     List<String> targets = zd.getTargets();
/*  731 */     if ((targets != null) && (targets.size() > 0)) {
/*  732 */       attrs.put("zimbraZimletTarget", targets);
/*      */     }
/*      */     
/*  735 */     String disableZimletUndeploy = zd.getDisableUIUndeploy();
/*  736 */     if ((disableZimletUndeploy != null) && (disableZimletUndeploy.equalsIgnoreCase("true"))) {
/*  737 */       attrs.put("zimbraAdminExtDisableUIUndeploy", "TRUE");
/*      */     }
/*      */     
/*  740 */     if (zd.isExtension()) {
/*  741 */       attrs.put("zimbraZimletIsExtension", "TRUE");
/*      */     } else {
/*  743 */       attrs.put("zimbraZimletIsExtension", "FALSE");
/*      */     }
/*      */     
/*  746 */     ZimbraLog.zimlet.info("Deploying Zimlet " + zimletName + " in LDAP.");
/*      */     
/*      */ 
/*  749 */     Provisioning prov = Provisioning.getInstance();
/*  750 */     Zimlet zim = prov.getZimlet(zimletName);
/*  751 */     if (zim == null) {
/*  752 */       zim = prov.createZimlet(zimletName, attrs);
/*      */     } else {
/*  754 */       prov.modifyAttrs(zim, attrs);
/*      */     }
/*  756 */     return zim;
/*      */   }
/*      */   
/*      */   private static void writeFile(byte[] src, File dest) throws IOException {
/*  760 */     dest.createNewFile();
/*  761 */     ByteArrayInputStream bais = new ByteArrayInputStream(src);
/*  762 */     FileOutputStream fos = new FileOutputStream(dest);
/*  763 */     ByteUtil.copy(bais, true, fos, true);
/*      */   }
/*      */   
/*      */   private static void deleteFile(File f) {
/*  767 */     if (f.isDirectory()) {
/*  768 */       for (File sub : f.listFiles()) {
/*  769 */         deleteFile(sub);
/*      */       }
/*      */     }
/*  772 */     f.delete();
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void undeployZimletLocally(String zimlet)
/*      */     throws ServiceException
/*      */   {
/*  783 */     ZimbraLog.zimlet.info("Uninstalling Zimlet " + zimlet + " from LDAP.");
/*  784 */     Provisioning prov = Provisioning.getInstance();
/*  785 */     Zimlet z = prov.getZimlet(zimlet);
/*  786 */     if (z != null) {
/*  787 */       List<Cos> cos = prov.getAllCos();
/*  788 */       for (Cos c : cos) {
/*      */         try {
/*  790 */           deactivateZimlet(zimlet, c.getName());
/*      */         } catch (Exception e) {
/*  792 */           ZimbraLog.zimlet.warn("Error deactiving Zimlet " + zimlet + " in LDAP.", e);
/*      */         }
/*      */       }
/*      */       try {
/*  796 */         prov.deleteZimlet(zimlet);
/*      */       } catch (ServiceException se) {
/*  798 */         z = prov.getZimlet(zimlet);
/*  799 */         if (z != null) {
/*  800 */           ZimbraLog.zimlet.warn("Error deleting Zimlet " + zimlet + " in LDAP.", se);
/*      */         }
/*      */       }
/*      */     }
/*      */     
/*  805 */     ZimletFile zf = (ZimletFile)sZimlets.get(zimlet);
/*  806 */     if (zf != null) {
/*  807 */       sZimlets.remove(zimlet);
/*      */     }
/*  809 */     ZimbraLog.zimlet.info("undeploying zimlet %s", new Object[] { zimlet });
/*      */     try {
/*  811 */       File zimletDir = getZimletRootDir(zimlet);
/*  812 */       FileUtil.deleteDir(zimletDir);
/*  813 */       ZimbraLog.zimlet.info("zimlet directory %s is deleted", new Object[] { zimletDir.getName() });
/*      */     } catch (IOException e) {
/*  815 */       throw ServiceException.FAILURE("error occurred when deleting zimlet directory", e);
/*      */     } catch (ZimletException e) {
/*  817 */       throw ServiceException.FAILURE("error occurred when deleting zimlet directory", e);
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void activateZimlet(String zimlet, String cos)
/*      */     throws ServiceException, ZimletException
/*      */   {
/*  830 */     ZimbraLog.zimlet.info("Adding Zimlet " + zimlet + " to COS " + cos);
/*  831 */     Provisioning prov = Provisioning.getInstance();
/*  832 */     Cos c = prov.get(Key.CosBy.name, cos);
/*  833 */     if (c == null) {
/*  834 */       throw ZimletException.CANNOT_ACTIVATE("no such cos " + cos, null);
/*      */     }
/*  836 */     Map<String, Object> attrs = new HashMap();
/*  837 */     attrs.put("+zimbraZimletAvailableZimlets", zimlet);
/*  838 */     prov.modifyAttrs(c, attrs);
/*  839 */     ZimletConfig zc = getZimletConfig(zimlet);
/*  840 */     if (zc == null) {
/*  841 */       return;
/*      */     }
/*  843 */     String allowedDomains = zc.getConfigValue("allowedDomains");
/*  844 */     if (allowedDomains != null) {
/*  845 */       addAllowedDomains(allowedDomains, cos);
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void deactivateZimlet(String zimlet, String cos)
/*      */     throws ServiceException, ZimletException
/*      */   {
/*  858 */     ZimbraLog.zimlet.info("Removing Zimlet " + zimlet + " from COS " + cos);
/*  859 */     Provisioning prov = Provisioning.getInstance();
/*  860 */     Cos c = prov.get(Key.CosBy.name, cos);
/*  861 */     if (c == null) {
/*  862 */       throw ZimletException.CANNOT_DEACTIVATE("no such cos " + cos, null);
/*      */     }
/*  864 */     Map<String, Object> attrs = new HashMap();
/*  865 */     attrs.put("-zimbraZimletAvailableZimlets", zimlet);
/*  866 */     prov.modifyAttrs(c, attrs);
/*  867 */     ZimletConfig zc = getZimletConfig(zimlet);
/*  868 */     if (zc == null) {
/*  869 */       return;
/*      */     }
/*  871 */     String domains = zc.getConfigValue("allowedDomains");
/*  872 */     if (domains == null) {
/*  873 */       return;
/*      */     }
/*  875 */     String[] domainArray = domains.toLowerCase().split(",");
/*  876 */     Set<String> domainsToRemove = new HashSet();
/*  877 */     for (String d : domainArray) {
/*  878 */       domainsToRemove.add(d);
/*      */     }
/*  880 */     String[] zimlets = getAvailableZimlets(c).getZimletNamesAsArray();
/*  881 */     for (String z : zimlets)
/*  882 */       if (!z.equals(zimlet))
/*      */       {
/*      */ 
/*  885 */         zc = getZimletConfig(z);
/*  886 */         if (zc != null)
/*      */         {
/*      */ 
/*  889 */           domains = zc.getConfigValue("allowedDomains");
/*  890 */           if (domains != null)
/*      */           {
/*      */ 
/*  893 */             domainArray = domains.toLowerCase().split(",");
/*  894 */             for (String d : domainArray)
/*  895 */               domainsToRemove.remove(d);
/*      */           }
/*      */         } }
/*  898 */     if (!domainsToRemove.isEmpty()) {
/*  899 */       removeAllowedDomains(domainsToRemove, cos);
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void setZimletEnable(String zimlet, boolean enabled)
/*      */     throws ZimletException
/*      */   {
/*  912 */     Provisioning prov = Provisioning.getInstance();
/*      */     try {
/*  914 */       Zimlet z = prov.getZimlet(zimlet);
/*  915 */       if (z == null) {
/*  916 */         throw AccountServiceException.NO_SUCH_ZIMLET(zimlet);
/*      */       }
/*  918 */       Map<String, String> attr = new HashMap();
/*  919 */       attr.put("zimbraZimletEnabled", enabled ? "TRUE" : "FALSE");
/*  920 */       prov.modifyAttrs(z, attr);
/*      */     } catch (Exception e) {
/*  922 */       if (enabled) {
/*  923 */         throw ZimletException.CANNOT_ENABLE(zimlet, e);
/*      */       }
/*  925 */       throw ZimletException.CANNOT_DISABLE(zimlet, e);
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void enableZimlet(String zimlet)
/*      */     throws ZimletException
/*      */   {
/*  938 */     ZimbraLog.zimlet.info("Enabling Zimlet " + zimlet);
/*  939 */     setZimletEnable(zimlet, true);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void disableZimlet(String zimlet)
/*      */     throws ZimletException
/*      */   {
/*  950 */     ZimbraLog.zimlet.info("Disabling Zimlet " + zimlet);
/*  951 */     setZimletEnable(zimlet, false);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void aclZimlet(String zimlet, String[] args)
/*      */     throws ServiceException, ZimletException
/*      */   {
/*  963 */     for (int i = 2; i < args.length; i += 2) {
/*  964 */       String cos = args[i];
/*  965 */       String action = args[(i + 1)].toLowerCase();
/*  966 */       if (action.equals("grant")) {
/*  967 */         activateZimlet(zimlet, cos);
/*  968 */       } else if (action.equals("deny")) {
/*  969 */         deactivateZimlet(zimlet, cos);
/*      */       } else {
/*  971 */         throw ZimletException.ZIMLET_HANDLER_ERROR("invalid acl command " + args[(i + 1)]);
/*      */       }
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void listAcls(String zimlet)
/*      */     throws ServiceException, ZimletException
/*      */   {
/*  984 */     System.out.println("Listing COS entries for Zimlet " + zimlet + "...");
/*  985 */     Provisioning prov = Provisioning.getInstance();
/*  986 */     for (Cos cos : prov.getAllCos()) {
/*  987 */       String[] zimlets = getAvailableZimlets(cos).getZimletNamesAsArray();
/*  988 */       for (int i = 0; i < zimlets.length; i++) {
/*  989 */         if (zimlets[i].equals(zimlet)) {
/*  990 */           System.out.println("\t" + cos.getName());
/*  991 */           break;
/*      */         }
/*      */       }
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void listInstalledZimletsOnHost(boolean everything)
/*      */   {
/* 1004 */     loadZimlets();
/* 1005 */     ZimletFile[] zimlets = (ZimletFile[])sZimlets.values().toArray(new ZimletFile[0]);
/* 1006 */     Arrays.sort(zimlets);
/* 1007 */     for (int i = 0; i < zimlets.length; i++)
/*      */     {
/* 1009 */       System.out.print("\t" + zimlets[i].getZimletName());
/*      */       try {
/* 1011 */         ZimletDescription zd = zimlets[i].getZimletDescription();
/* 1012 */         boolean isExtension = (zd != null) && (zd.isExtension());
/* 1013 */         if ((isExtension) && (everything)) {
/* 1014 */           System.out.print(" (ext)");
/*      */         }
/*      */       } catch (Exception e) {
/* 1017 */         ZimbraLog.zimlet.warn("error reading zimlet : " + zimlets[i].getName(), e);
/*      */       }
/* 1019 */       System.out.println();
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void listInstalledZimletsInLdap(boolean everything)
/*      */     throws ServiceException, ZimletException
/*      */   {
/* 1030 */     Provisioning prov = Provisioning.getInstance();
/* 1031 */     for (Zimlet z : prov.listAllZimlets()) {
/* 1032 */       boolean isExtension = z.isExtension();
/* 1033 */       if ((everything) || (!isExtension))
/*      */       {
/*      */ 
/* 1036 */         System.out.print("\t" + z.getName());
/* 1037 */         if (!z.isEnabled()) {
/* 1038 */           System.out.print(" (disabled)");
/*      */         }
/* 1040 */         if (isExtension) {
/* 1041 */           System.out.print(" (ext)");
/*      */         }
/* 1043 */         System.out.println();
/*      */       }
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void listZimletsInCos()
/*      */     throws ServiceException, ZimletException
/*      */   {
/* 1054 */     Provisioning prov = Provisioning.getInstance();
/* 1055 */     for (Cos cos : prov.getAllCos()) {
/* 1056 */       System.out.println("  " + cos.getName() + ":");
/* 1057 */       String[] zimlets = getAvailableZimlets(cos).getZimletNamesAsArray();
/* 1058 */       Arrays.sort(zimlets);
/* 1059 */       for (int i = 0; i < zimlets.length; i++) {
/* 1060 */         System.out.println("\t" + zimlets[i]);
/*      */       }
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void listAllZimlets(boolean everything)
/*      */     throws ServiceException, ZimletException
/*      */   {
/* 1072 */     System.out.println("Installed Zimlet files on this host:");
/* 1073 */     listInstalledZimletsOnHost(everything);
/* 1074 */     System.out.println("Installed Zimlets in LDAP:");
/* 1075 */     listInstalledZimletsInLdap(everything);
/* 1076 */     System.out.println("Available Zimlets in COS:");
/* 1077 */     listZimletsInCos();
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   public static void dumpConfig(String zimlet)
/*      */     throws IOException, ZimletException
/*      */   {
/* 1089 */     ZimletFile zf = new ZimletFile(zimlet);
/* 1090 */     String config = zf.getZimletConfigString();
/* 1091 */     System.out.println(config);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   private static void installConfig(ZimletConfig zc)
/*      */     throws ServiceException, IOException, ZimletException
/*      */   {
/* 1103 */     String zimletName = zc.getName();
/* 1104 */     ZimbraLog.zimlet.info("Installing Zimlet config for " + zimletName);
/* 1105 */     String configString = zc.toXMLString();
/* 1106 */     updateZimletConfig(zimletName, configString);
/* 1107 */     String allowedDomains = zc.getConfigValue("allowedDomains");
/* 1108 */     if (allowedDomains != null) {
/* 1109 */       addAllowedDomains(allowedDomains, "default");
/*      */     }
/*      */   }
/*      */   
/*      */   public static void installConfig(String config) throws ServiceException, IOException, ZimletException {
/* 1114 */     installConfig(new ZimletConfig(config));
/*      */   }
/*      */   
/*      */   public static void addAllowedDomains(String domains, String cosName) throws ServiceException {
/* 1118 */     Provisioning prov = Provisioning.getInstance();
/* 1119 */     Cos cos = prov.get(Key.CosBy.name, cosName);
/* 1120 */     Set<String> domainSet = cos.getMultiAttrSet("zimbraProxyAllowedDomains");
/* 1121 */     String[] domainArray = domains.toLowerCase().split(",");
/* 1122 */     for (int i = 0; i < domainArray.length; i++) {
/* 1123 */       domainSet.add(domainArray[i]);
/*      */     }
/* 1125 */     Map<String, String[]> newlist = new HashMap();
/* 1126 */     newlist.put("zimbraProxyAllowedDomains", domainSet.toArray(new String[0]));
/* 1127 */     prov.modifyAttrs(cos, newlist);
/*      */   }
/*      */   
/*      */   public static void removeAllowedDomains(Set<String> domains, String cosName) throws ServiceException {
/* 1131 */     Provisioning prov = Provisioning.getInstance();
/* 1132 */     Cos cos = prov.get(Key.CosBy.name, cosName);
/* 1133 */     Set<String> domainSet = cos.getMultiAttrSet("zimbraProxyAllowedDomains");
/* 1134 */     String[] domainArray = (String[])domains.toArray(new String[0]);
/* 1135 */     for (int i = 0; i < domainArray.length; i++) {
/* 1136 */       domainSet.remove(domainArray[i]);
/*      */     }
/* 1138 */     Map<String, String[]> newlist = new HashMap();
/* 1139 */     newlist.put("zimbraProxyAllowedDomains", domainSet.toArray(new String[0]));
/* 1140 */     prov.modifyAttrs(cos, newlist);
/*      */   }
/*      */   
/*      */   public static void listPriority() throws ServiceException {
/* 1144 */     List<Zimlet> plist = orderZimletsByPriority();
/* 1145 */     System.out.println("Pri\tZimlet");
/* 1146 */     for (int i = 0; i < plist.size(); i++) {
/* 1147 */       System.out.println(i + "\t" + ((Zimlet)plist.get(i)).getName());
/*      */     }
/*      */   }
/*      */   
/*      */   public static void setPriority(String zimlet, int priority) throws ServiceException {
/* 1152 */     List<Zimlet> plist = orderZimletsByPriority();
/* 1153 */     Provisioning prov = Provisioning.getInstance();
/* 1154 */     Zimlet z = prov.getZimlet(zimlet);
/* 1155 */     if (z == null) {
/* 1156 */       throw AccountServiceException.NO_SUCH_ZIMLET(zimlet);
/*      */     }
/* 1158 */     setPriority(z, priority, plist);
/*      */   }
/*      */   
/*      */   private static void setPriority(Zimlet zimlet, String priority) throws ServiceException
/*      */   {
/* 1163 */     Provisioning prov = Provisioning.getInstance();
/* 1164 */     Map<String, String> attr = new HashMap();
/* 1165 */     attr.put("zimbraZimletPriority", priority);
/* 1166 */     prov.modifyAttrs(zimlet, attr);
/*      */   }
/*      */   
/*      */   public static void setPriority(Zimlet z, int priority, List<Zimlet> plist)
/*      */     throws ServiceException
/*      */   {
/* 1172 */     for (Zimlet zim : plist) {
/* 1173 */       if (zim.compareTo(z) == 0) {
/* 1174 */         plist.remove(zim);
/* 1175 */         break;
/*      */       }
/*      */     }
/*      */     
/* 1179 */     if (priority == P_MAX) {
/* 1180 */       priority = plist.size();
/*      */     }
/*      */     
/* 1183 */     if (priority == 0) {
/* 1184 */       Version newPriority = new Version("0");
/* 1185 */       setPriority(z, newPriority.toString());
/* 1186 */       plist.add(0, z);
/* 1187 */       if (plist.size() > 1)
/*      */       {
/* 1189 */         Zimlet p0zimlet = (Zimlet)plist.get(1);
/* 1190 */         setPriority(p0zimlet, 1, plist);
/*      */       }
/*      */     }
/*      */     else {
/* 1194 */       Zimlet oneAbove = (Zimlet)plist.get(priority - 1);
/* 1195 */       String pString = oneAbove.getPriority();
/* 1196 */       if (pString == null)
/*      */       {
/*      */ 
/* 1199 */         pString = Integer.toString(priority);
/*      */       }
/* 1201 */       Version newPriority = new Version(pString);
/* 1202 */       if (priority < plist.size())
/*      */       {
/* 1204 */         Zimlet oneBelow = (Zimlet)plist.get(priority);
/* 1205 */         pString = oneBelow.getPriority();
/* 1206 */         if (pString == null) {
/* 1207 */           pString = Integer.toString(priority + 2);
/*      */         }
/* 1209 */         Version nextPriority = new Version(pString);
/* 1210 */         if (newPriority.compareTo(nextPriority) < 0) {
/* 1211 */           newPriority.increment(nextPriority);
/*      */         }
/*      */         else
/*      */         {
/* 1215 */           newPriority.increment();
/* 1216 */           setPriority(z, newPriority.toString());
/* 1217 */           plist.add(priority, z);
/* 1218 */           setPriority(oneBelow, priority + 1, plist);
/* 1219 */           return;
/*      */         }
/*      */       }
/*      */       else {
/* 1223 */         newPriority.increment();
/*      */       }
/* 1225 */       setPriority(z, newPriority.toString());
/*      */     }
/*      */     try
/*      */     {
/* 1229 */       flushCache();
/*      */     } catch (Exception e) {
/* 1231 */       Throwable t = e.getCause();
/* 1232 */       if ((t instanceof ServiceException)) {
/* 1233 */         throw ((ServiceException)t);
/*      */       }
/* 1235 */       if ((e instanceof IOException)) {
/* 1236 */         t = e;
/*      */       }
/* 1238 */       throw ServiceException.FAILURE("unable to set priority", t);
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */   private static void deployZimletRemotely(String zimletFile, String adminURL, String uploadURL, boolean synchronous)
/*      */     throws ServiceException, IOException
/*      */   {
/* 1247 */     File zf = new File(zimletFile);
/* 1248 */     ZimletSoapUtil soapUtil; if ((adminURL != null) && (uploadURL != null)) {
/* 1249 */       ZimletSoapUtil soapUtil = new ZimletSoapUtil(adminURL, uploadURL, null, null);
/* 1250 */       soapUtil.deployZimletOnServer(zf.getName(), ByteUtil.getContent(zf), true);
/*      */     }
/*      */     else {
/* 1253 */       soapUtil = new ZimletSoapUtil();
/* 1254 */       soapUtil.mSynchronous = synchronous;
/* 1255 */       List<Server> allServers = Provisioning.getInstance().getAllServers();
/* 1256 */       for (Server server : allServers) {
/* 1257 */         if (!server.isLocalServer()) {
/* 1258 */           soapUtil.deployZimletRemotely(server, zf.getName(), ByteUtil.getContent(zf), null, true);
/*      */         }
/*      */       }
/*      */     }
/*      */   }
/*      */   
/*      */   private static void undeployZimletRemotely(String zimlet) throws ServiceException {
/* 1265 */     ZimletSoapUtil soapUtil = new ZimletSoapUtil();
/* 1266 */     List<Server> allServers = Provisioning.getInstance().getAllServers();
/* 1267 */     for (Server server : allServers) {
/* 1268 */       if (!server.isLocalServer()) {
/* 1269 */         soapUtil.undeployZimletRemotely(server, zimlet);
/*      */       }
/*      */     }
/*      */   }
/*      */   
/*      */   public static void showInfo(String zimlet) throws ServiceException, ZimletException, IOException {
/* 1275 */     Provisioning prov = Provisioning.getInstance();
/* 1276 */     Zimlet z = prov.getZimlet(zimlet);
/* 1277 */     if (z == null) {
/* 1278 */       throw AccountServiceException.NO_SUCH_ZIMLET(zimlet);
/*      */     }
/* 1280 */     List<Zimlet> plist = orderZimletsByPriority();
/* 1281 */     int pri = 0;
/* 1282 */     for (Zimlet zp : plist) {
/* 1283 */       if (zp.compareTo(z) == 0) {
/*      */         break;
/*      */       }
/* 1286 */       pri++;
/*      */     }
/* 1288 */     System.out.println("Zimlet " + z.getName());
/* 1289 */     System.out.println("         Version: " + z.getAttr("zimbraZimletVersion"));
/* 1290 */     System.out.println("     Description: " + z.getDescription());
/* 1291 */     System.out.println("        Priority: " + pri);
/* 1292 */     System.out.println("         Enabled: " + (z.isEnabled() ? "true" : "false"));
/* 1293 */     System.out.println("Indexing Enabled: " + (z.isIndexingEnabled() ? "true" : "false"));
/* 1294 */     if (z.isExtension()) {
/* 1295 */       System.out.println("       Extension: true");
/*      */     }
/* 1297 */     String cosList = null;
/* 1298 */     for (Cos cos : prov.getAllCos()) {
/* 1299 */       for (String zc : getAvailableZimlets(cos).getZimletNamesAsArray())
/* 1300 */         if (zc.compareTo(zimlet) == 0)
/*      */         {
/*      */ 
/* 1303 */           if (cosList == null) {
/* 1304 */             cosList = cos.getName(); break;
/*      */           }
/* 1306 */           cosList = cosList + ", " + cos.getName();
/*      */           
/* 1308 */           break;
/*      */         }
/*      */     }
/* 1311 */     System.out.println("Activated in COS: " + cosList);
/* 1312 */     System.out.println("          Config: " + z.getHandlerConfig());
/* 1313 */     ZimletFile zf = getZimlet(zimlet);
/* 1314 */     if (zf == null) {
/* 1315 */       System.out.println("*** Zimlet file is missing on this machine");
/*      */     } else {
/* 1317 */       ZimletDescription desc = zf.getZimletDescription();
/* 1318 */       String val = desc.getRegexString();
/* 1319 */       if (val != null) {
/* 1320 */         System.out.println("           RegEx: " + val);
/*      */       }
/* 1322 */       val = desc.getContentObjectAsXML();
/* 1323 */       if (val != null) {
/* 1324 */         System.out.println("  Content Object: " + val);
/*      */       }
/* 1326 */       val = desc.getPanelItemAsXML();
/* 1327 */       if (val != null) {
/* 1328 */         System.out.println("      Panel Item: " + val);
/*      */       }
/* 1330 */       val = null;
/* 1331 */       for (String script : desc.getScripts()) {
/* 1332 */         if (val == null) {
/* 1333 */           val = script;
/*      */         } else {
/* 1335 */           val = val + ", " + script;
/*      */         }
/*      */       }
/* 1338 */       if (val != null) {
/* 1339 */         System.out.println("         Scripts: " + val);
/*      */       }
/* 1341 */       val = null;
/* 1342 */       for (String css : desc.getStyleSheets()) {
/* 1343 */         if (val == null) {
/* 1344 */           val = css;
/*      */         } else {
/* 1346 */           val = val + ", " + css;
/*      */         }
/*      */       }
/* 1349 */       if (val != null) {
/* 1350 */         System.out.println("             CSS: " + val);
/*      */       }
/* 1352 */       val = null;
/* 1353 */       for (String target : desc.getTargets()) {
/* 1354 */         if (val == null) {
/* 1355 */           val = target;
/*      */         } else {
/* 1357 */           val = val + ", " + target;
/*      */         }
/*      */       }
/* 1360 */       if (val != null) {
/* 1361 */         System.out.println("         Targets: " + val);
/*      */       }
/*      */     }
/*      */   }
/*      */   
/*      */   public static void createZip(String dirName, String descFile) throws IOException {
/* 1367 */     File dir = new File(dirName);
/* 1368 */     if ((!dir.exists()) || (!dir.isDirectory())) {
/* 1369 */       throw new IOException("directory does not exist: " + dirName);
/*      */     }
/* 1371 */     String target = descFile;
/* 1372 */     boolean found = false;
/* 1373 */     for (String f : dir.list()) {
/* 1374 */       if (target != null) {
/* 1375 */         if (target.compareTo(f) == 0) {
/* 1376 */           found = true;
/* 1377 */           break;
/*      */         }
/* 1379 */       } else if ((f.endsWith(".xml")) && (f.substring(0, f.length() - 4).compareTo(dir.getName()) == 0)) {
/* 1380 */         target = f;
/* 1381 */         found = true;
/* 1382 */         break;
/*      */       }
/*      */     }
/* 1385 */     if (!found) {
/* 1386 */       throw new IOException("Zimlet description not found, or not named correctly.");
/*      */     }
/* 1388 */     String manifest = "Manifest-Version: 1.0\nZimlet-Description-File: " + target + "\n";
/* 1389 */     JarOutputStream out = new JarOutputStream(new FileOutputStream(target.substring(0, target.length() - 4) + ".zip"), new Manifest(new ByteArrayInputStream(manifest.getBytes("UTF-8"))));
/*      */     
/* 1391 */     for (File f : dir.listFiles()) {
/* 1392 */       addZipEntry(out, f, null);
/*      */     }
/* 1394 */     out.close();
/*      */   }
/*      */   
/*      */   private static void addZipEntry(ZipOutputStream out, File file, String path) throws IOException {
/* 1398 */     String name = path + "/" + file.getName();
/* 1399 */     if (file.isDirectory()) {
/* 1400 */       for (File f : file.listFiles()) {
/* 1401 */         addZipEntry(out, f, name);
/*      */       }
/* 1403 */       return;
/*      */     }
/* 1405 */     ZipEntry entry = new ZipEntry(name);
/* 1406 */     entry.setMethod(0);
/* 1407 */     entry.setSize(file.length());
/* 1408 */     entry.setCompressedSize(file.length());
/* 1409 */     entry.setCrc(computeCRC32(file));
/* 1410 */     out.putNextEntry(entry);
/* 1411 */     ByteUtil.copy(new FileInputStream(file), true, out, false);
/* 1412 */     out.closeEntry();
/*      */   }
/*      */   
/*      */   private static long computeCRC32(File file) throws IOException {
/* 1416 */     byte[] buf = new byte[32768];
/* 1417 */     CRC32 crc = new CRC32();
/* 1418 */     crc.reset();
/* 1419 */     FileInputStream fis = null;
/*      */     try {
/* 1421 */       fis = new FileInputStream(file);
/*      */       int bytesRead;
/* 1423 */       while ((bytesRead = fis.read(buf)) != -1) {
/* 1424 */         crc.update(buf, 0, bytesRead);
/*      */       }
/* 1426 */       return crc.getValue();
/*      */     } finally {
/* 1428 */       if (fis != null) {
/*      */         try {
/* 1430 */           fis.close();
/*      */         } catch (IOException e) {}
/*      */       }
/*      */     }
/*      */   }
/*      */   
/*      */   private static void test() {
/* 1437 */     String ZIMLET_URL = "^/service/zimlet/([^/\\?]+)[/\\?]?.*$";
/* 1438 */     String t1 = "/service/zimlet/po";
/* 1439 */     String t2 = "/service/zimlet/foo?123";
/* 1440 */     Pattern mPattern = Pattern.compile(ZIMLET_URL);
/* 1441 */     Matcher matcher = mPattern.matcher(t1);
/* 1442 */     if (matcher.matches()) {
/* 1443 */       System.out.println(matcher.group(1));
/*      */     }
/* 1445 */     matcher = mPattern.matcher(t2);
/* 1446 */     if (matcher.matches()) {
/* 1447 */       System.out.println(matcher.group(1));
/*      */     }
/*      */   }
/*      */   
/*      */   public static class ZimletSoapUtil {
/*      */     private String mUsername;
/*      */     private String mPassword;
/*      */     private String mAttachmentId;
/*      */     private String mAdminURL;
/*      */     private String mUploadURL;
/*      */     private ZAuthToken mAuth;
/*      */     private SoapHttpTransport mTransport;
/*      */     private boolean mSynchronous;
/*      */     private String mStatus;
/*      */     
/*      */     public ZimletSoapUtil() throws ServiceException {
/* 1463 */       initZimletSoapUtil(null, null);
/*      */     }
/*      */     
/*      */     public ZimletSoapUtil(String adminURL, String uploadURL, String username, String password) throws ServiceException {
/* 1467 */       this.mAdminURL = adminURL;
/* 1468 */       this.mUploadURL = uploadURL;
/* 1469 */       initZimletSoapUtil(username, password);
/*      */     }
/*      */     
/*      */     private void initZimletSoapUtil(String username, String password) throws ServiceException {
/* 1473 */       this.mUsername = (username != null ? username : LC.zimbra_ldap_user.value());
/* 1474 */       this.mPassword = (password != null ? password : LC.zimbra_ldap_password.value());
/* 1475 */       this.mAuth = null;
/* 1476 */       String adminURL = this.mAdminURL;
/* 1477 */       if (adminURL == null) {
/* 1478 */         String server = LC.zimbra_zmprov_default_soap_server.value();
/* 1479 */         adminURL = URLUtil.getAdminURL(server);
/*      */       }
/* 1481 */       SoapProvisioning sp = new SoapProvisioning();
/* 1482 */       sp.soapSetURI(adminURL);
/* 1483 */       sp.soapAdminAuthenticate(this.mUsername, this.mPassword);
/*      */     }
/*      */     
/*      */     public ZimletSoapUtil(ZAuthToken auth) {
/* 1487 */       this.mAuth = auth;
/*      */     }
/*      */     
/*      */     public void deployZimletRemotely(Server server, String zimlet, byte[] data, ZimletUtil.DeployListener listener, boolean flushCache) throws ServiceException
/*      */     {
/* 1492 */       if (server.hasMailClientService()) {
/* 1493 */         ZimbraLog.zimlet.info("Deploying on service node %s", new Object[] { server.getName() });
/* 1494 */         deployZimletOnServiceNode(zimlet, data, server, listener, flushCache);
/*      */       } else {
/* 1496 */         ZimbraLog.zimlet.info("Deploying on ui node %s", new Object[] { server.getName() });
/* 1497 */         deployZimletOnUiNode(zimlet, data, server, listener, flushCache);
/*      */       }
/*      */     }
/*      */     
/*      */     public void undeployZimletRemotely(Server server, String zimlet) throws ServiceException {
/* 1502 */       ZimbraLog.zimlet.info("Undeploying on %s", new Object[] { server.getName() });
/* 1503 */       if (server.hasMailClientService()) {
/* 1504 */         undeployZimletOnServiceNode(server, zimlet);
/*      */       } else {
/* 1506 */         undeployZimletOnUiNode(server, zimlet);
/*      */       }
/*      */     }
/*      */     
/*      */     public void deployZimletOnServer(String zimlet, byte[] data, boolean flushCache) throws ServiceException {
/* 1511 */       this.mTransport = null;
/*      */       try {
/* 1513 */         this.mTransport = new SoapHttpTransport(this.mAdminURL);
/* 1514 */         auth();
/* 1515 */         this.mTransport.setAuthToken(this.mAuth);
/* 1516 */         URL url = new URL(this.mUploadURL);
/* 1517 */         this.mAttachmentId = postAttachment(this.mUploadURL, zimlet, data, url.getHost());
/*      */         
/* 1519 */         soapDeployZimlet(flushCache);
/* 1520 */         if (this.mSynchronous) {
/* 1521 */           ZimbraLog.zimlet.info("Deploy status: %s", new Object[] { this.mStatus });
/*      */         } else {
/* 1523 */           ZimbraLog.zimlet.info("Deploy initiated. Check the server's mailbox.log for the status.");
/*      */         }
/*      */       } catch (Exception e) {
/* 1526 */         ZimbraLog.zimlet.info("deploy failed on %s", this.mAdminURL, e);
/* 1527 */         if ((e instanceof ServiceException)) {
/* 1528 */           throw ((ServiceException)e);
/*      */         }
/* 1530 */         throw ServiceException.FAILURE("Unable to deploy Zimlet " + zimlet + " on " + this.mAdminURL, e);
/*      */       }
/*      */       finally {
/* 1533 */         if (this.mTransport != null) {
/* 1534 */           this.mTransport.shutdown();
/*      */         }
/*      */       }
/*      */     }
/*      */     
/*      */     public void deployZimletOnServiceNode(String zimlet, byte[] data, Server server, ZimletUtil.DeployListener listener, boolean flushCache) throws ServiceException
/*      */     {
/* 1541 */       this.mTransport = null;
/*      */       try {
/* 1543 */         String adminUrl = URLUtil.getAdminURL(server, "/service/admin/soap/");
/* 1544 */         this.mTransport = new SoapHttpTransport(adminUrl);
/*      */         
/*      */ 
/* 1547 */         if (this.mAuth == null) {
/* 1548 */           auth();
/*      */         }
/* 1550 */         this.mTransport.setAuthToken(this.mAuth);
/*      */         
/*      */ 
/* 1553 */         String uploadUrl = URLUtil.getAdminURL(server, "/service/upload?fmt=raw");
/* 1554 */         ZimbraLog.zimlet.info("post to server %s, data size %d", new Object[] { server.getName(), Integer.valueOf(data.length) });
/* 1555 */         this.mAttachmentId = postAttachment(uploadUrl, zimlet, data, server.getName());
/*      */         
/*      */ 
/* 1558 */         soapDeployZimlet(flushCache);
/* 1559 */         if (this.mSynchronous) {
/* 1560 */           ZimbraLog.zimlet.info("Deploy status: %s", new Object[] { this.mStatus });
/*      */         } else {
/* 1562 */           ZimbraLog.zimlet.info("Deploy initiated. Check the server %s's mailbox.log for the status.", new Object[] { server.getName() });
/*      */         }
/*      */         
/* 1565 */         if (listener != null) {
/* 1566 */           listener.markFinished(server);
/*      */         }
/*      */       } catch (Exception e) {
/* 1569 */         ZimbraLog.zimlet.info("deploy failed on service node %s", server.getName(), e);
/* 1570 */         if (listener != null) {
/* 1571 */           listener.markFailed(server, e);
/* 1572 */         } else { if ((e instanceof ServiceException)) {
/* 1573 */             throw ((ServiceException)e);
/*      */           }
/* 1575 */           throw ServiceException.FAILURE("Unable to deploy Zimlet " + zimlet + " on " + server.getName(), e);
/*      */         }
/*      */       } finally {
/* 1578 */         if (this.mTransport != null) {
/* 1579 */           this.mTransport.shutdown();
/*      */         }
/*      */       }
/*      */     }
/*      */     
/*      */     void deployZimletOnUiNode(String zimlet, byte[] data, Server server, ZimletUtil.DeployListener listener, boolean flushCache) throws ServiceException
/*      */     {
/*      */       try {
/* 1587 */         WebClientServiceUtil.sendDeployZimletRequestToUiNode(server, zimlet, data);
/* 1588 */         if (this.mSynchronous) {
/* 1589 */           ZimbraLog.zimlet.info("Deploy status: %s", new Object[] { this.mStatus });
/*      */         } else {
/* 1591 */           ZimbraLog.zimlet.info("Deploy initiated. Check the server %s's mailbox.log for the status.", new Object[] { server.getName() });
/*      */         }
/*      */         
/* 1594 */         if (listener != null) {
/* 1595 */           listener.markFinished(server);
/*      */         }
/*      */       } catch (Exception e) {
/* 1598 */         ZimbraLog.zimlet.info("deploy failed on ui node %s", server.getName(), e);
/* 1599 */         if (listener != null) {
/* 1600 */           listener.markFailed(server, e);
/* 1601 */         } else { if ((e instanceof ServiceException)) {
/* 1602 */             throw ((ServiceException)e);
/*      */           }
/* 1604 */           throw ServiceException.FAILURE("Unable to deploy Zimlet " + zimlet + " on " + server.getName(), e);
/*      */         }
/*      */       }
/*      */     }
/*      */     
/*      */     private void soapDeployZimlet(boolean flushCache) throws ServiceException, IOException {
/* 1610 */       Element.XMLElement req = new Element.XMLElement(AdminConstants.DEPLOY_ZIMLET_REQUEST);
/* 1611 */       req.addAttribute("action", "deploylocal");
/* 1612 */       req.addAttribute("flush", flushCache);
/* 1613 */       if (this.mSynchronous) {
/* 1614 */         req.addAttribute("synchronous", this.mSynchronous);
/*      */       }
/* 1616 */       req.addElement("content").addAttribute("aid", this.mAttachmentId);
/* 1617 */       Element res = this.mTransport.invoke(req);
/* 1618 */       if (this.mSynchronous) {
/* 1619 */         this.mStatus = res.getElement("progress").getAttribute("status", "");
/*      */       }
/*      */     }
/*      */     
/*      */     public void undeployZimletOnServiceNode(Server server, String zimlet) throws ServiceException {
/* 1624 */       this.mTransport = null;
/*      */       try {
/* 1626 */         String adminUrl = URLUtil.getAdminURL(server, "/service/admin/soap/");
/* 1627 */         this.mTransport = new SoapHttpTransport(adminUrl);
/*      */         
/*      */ 
/* 1630 */         if (this.mAuth == null) {
/* 1631 */           auth();
/*      */         }
/* 1633 */         this.mTransport.setAuthToken(this.mAuth);
/* 1634 */         Element.XMLElement req = new Element.XMLElement(AdminConstants.UNDEPLOY_ZIMLET_REQUEST);
/* 1635 */         req.addAttribute("action", "deploylocal");
/* 1636 */         req.addAttribute("name", zimlet);
/* 1637 */         this.mTransport.invoke(req);
/* 1638 */         ZimbraLog.zimlet.info("Undeploy initiated. Check the server %s's mailbox.log for the status.", new Object[] { server.getName() });
/*      */       }
/*      */       catch (Exception e) {
/* 1641 */         if ((e instanceof ServiceException)) {
/* 1642 */           throw ((ServiceException)e);
/*      */         }
/* 1644 */         throw ServiceException.FAILURE("Unable to undeploy Zimlet " + zimlet + " on " + server.getName(), e);
/*      */       }
/*      */       finally {
/* 1647 */         if (this.mTransport != null) {
/* 1648 */           this.mTransport.shutdown();
/*      */         }
/*      */       }
/*      */     }
/*      */     
/*      */     public void undeployZimletOnUiNode(Server server, String zimlet) throws ServiceException {
/*      */       try {
/* 1655 */         WebClientServiceUtil.sendUndeployZimletRequestToUiNode(server, zimlet);
/*      */       } catch (Exception e) {
/* 1657 */         ZimbraLog.zimlet.warn("undeployment failed on ui node %s", server.getName(), e);
/* 1658 */         if ((e instanceof ServiceException)) {
/* 1659 */           throw ((ServiceException)e);
/*      */         }
/* 1661 */         throw ServiceException.FAILURE("Unable to undeploy Zimlet " + zimlet + " on " + server.getName(), e);
/*      */       }
/*      */     }
/*      */     
/*      */     private String postAttachment(String uploadURL, String name, byte[] data, String domain)
/*      */       throws IOException, ZimletException
/*      */     {
/* 1668 */       String aid = null;
/*      */       
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/* 1674 */       HttpClient client = new HttpClient();
/* 1675 */       Map<String, String> cookieMap = this.mAuth.cookieMap(true);
/* 1676 */       if (cookieMap != null) {
/* 1677 */         HttpState state = new HttpState();
/* 1678 */         for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
/* 1679 */           state.addCookie(new Cookie(domain, (String)ck.getKey(), (String)ck.getValue(), "/", -1, false));
/*      */         }
/*      */         
/* 1682 */         client.setState(state);
/* 1683 */         client.getParams().setCookiePolicy("compatibility");
/*      */       }
/*      */       
/* 1686 */       PostMethod post = new PostMethod(uploadURL);
/* 1687 */       post.getParams().setSoTimeout((int)TimeUnit.MILLISECONDS.convert(LC.zimlet_deploy_timeout.intValue(), TimeUnit.SECONDS));
/*      */       
/* 1689 */       int statusCode = -1;
/*      */       try {
/* 1691 */         String contentType = URLConnection.getFileNameMap().getContentTypeFor(name);
/* 1692 */         Part[] parts = { new ByteArrayPart(data, name, contentType) };
/* 1693 */         post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
/* 1694 */         statusCode = HttpClientUtil.executeMethod(client, post);
/*      */         
/* 1696 */         if (statusCode == 200) {
/* 1697 */           String response = post.getResponseBodyAsString();
/*      */           
/*      */ 
/* 1700 */           String[] responseParts = response.split(",", 3);
/* 1701 */           if (responseParts.length == 3) {
/* 1702 */             aid = responseParts[2].trim();
/* 1703 */             if ((aid.startsWith("'")) || (aid.startsWith("\""))) {
/* 1704 */               aid = aid.substring(1);
/*      */             }
/* 1706 */             if ((aid.endsWith("'")) || (aid.endsWith("\""))) {
/* 1707 */               aid = aid.substring(0, aid.length() - 1);
/*      */             }
/*      */           }
/* 1710 */           if (aid == null) {
/* 1711 */             throw ZimletException.CANNOT_DEPLOY("Attachment post failed, unexpected response: " + response, null);
/*      */           }
/*      */         } else {
/* 1714 */           throw ZimletException.CANNOT_DEPLOY("Attachment post failed, status=" + statusCode, null);
/*      */         }
/*      */       } finally {
/* 1717 */         post.releaseConnection();
/*      */       }
/*      */       
/* 1720 */       return aid;
/*      */     }
/*      */     
/*      */     private void auth() throws ServiceException, IOException {
/* 1724 */       Element.XMLElement req = new Element.XMLElement(AdminConstants.AUTH_REQUEST);
/* 1725 */       req.addElement("name").setText(this.mUsername);
/* 1726 */       req.addElement("password").setText(this.mPassword);
/* 1727 */       Element resp = this.mTransport.invoke(req);
/*      */       
/* 1729 */       this.mAuth = new ZAuthToken(resp.getElement("authToken"), true);
/*      */     }
/*      */     
/*      */     private static class ByteArrayPart extends PartBase {
/*      */       private byte[] mData;
/*      */       private String mName;
/*      */       
/*      */       public ByteArrayPart(byte[] data, String name, String type) throws IOException {
/* 1737 */         super(type, "UTF-8", "binary");
/* 1738 */         this.mName = name;
/* 1739 */         this.mData = data;
/*      */       }
/*      */       
/*      */       protected void sendData(OutputStream out) throws IOException
/*      */       {
/* 1744 */         out.write(this.mData);
/*      */       }
/*      */       
/*      */       protected long lengthOfData() throws IOException
/*      */       {
/* 1749 */         return this.mData.length;
/*      */       }
/*      */       
/*      */       protected void sendDispositionHeader(OutputStream out) throws IOException
/*      */       {
/* 1754 */         super.sendDispositionHeader(out);
/* 1755 */         StringBuilder buf = new StringBuilder();
/* 1756 */         buf.append("; filename=\"").append(this.mName).append("\"");
/* 1757 */         out.write(buf.toString().getBytes());
/*      */       }
/*      */     }
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */   private static final String UNINSTALL_CMD = "uninstall";
/*      */   
/*      */   private static final String UNDEPLOY_CMD = "undeploy";
/*      */   
/*      */   private static final String LIST_CMD = "listzimlets";
/*      */   
/*      */   private static final String ACL_CMD = "acl";
/*      */   
/*      */   private static final String LIST_ACLS_CMD = "listacls";
/*      */   
/*      */   private static final String DUMP_CONFIG_CMD = "getconfigtemplate";
/*      */   
/*      */   private static final String INSTALL_CONFIG_CMD = "configure";
/*      */   
/*      */   private static final String LDAP_DEPLOY_CMD = "ldapdeploy";
/*      */   
/*      */   private static final String DEPLOY_CMD = "deploy";
/*      */   
/*      */   private static final String ENABLE_CMD = "enable";
/*      */   
/*      */   private static final String DISABLE_CMD = "disable";
/*      */   
/*      */   private static final String LIST_PRIORITY_CMD = "listpriority";
/*      */   
/*      */   private static final String SET_PRIORITY_CMD = "setpriority";
/*      */   
/*      */   private static final String INFO_CMD = "info";
/*      */   
/*      */   private static final String CREATE_ZIP_CMD = "createzip";
/*      */   
/*      */   private static final String TEST_CMD = "test";
/*      */   
/*      */   private static Map<String, Integer> mCommands;
/*      */   
/*      */   private static void addCommand(String cmd, int cmdId)
/*      */   {
/* 1800 */     mCommands.put(cmd, Integer.valueOf(cmdId));
/*      */   }
/*      */   
/*      */   private static void setup() {
/* 1804 */     mCommands = new HashMap();
/* 1805 */     addCommand("deploy", 18);
/* 1806 */     addCommand("undeploy", 11);
/* 1807 */     addCommand("install", 10);
/* 1808 */     addCommand("uninstall", 11);
/* 1809 */     addCommand("listzimlets", 12);
/* 1810 */     addCommand("acl", 13);
/* 1811 */     addCommand("listacls", 14);
/* 1812 */     addCommand("getconfigtemplate", 15);
/* 1813 */     addCommand("configure", 16);
/* 1814 */     addCommand("ldapdeploy", 17);
/* 1815 */     addCommand("enable", 19);
/* 1816 */     addCommand("disable", 20);
/* 1817 */     addCommand("listpriority", 21);
/* 1818 */     addCommand("setpriority", 22);
/* 1819 */     addCommand("info", 23);
/* 1820 */     addCommand("createzip", 24);
/* 1821 */     addCommand("test", 99);
/*      */   }
/*      */   
/*      */   private static void usage() {
/* 1825 */     System.out.println("Usage: zmzimletctl [-l] [-a <admin url> -u <upload url>] [command] [ zimlet.zip | config.xml | zimlet ]");
/*      */     
/* 1827 */     System.out.println("\tdeploy {zimlet.zip} - install, ldapDeploy, grant ACL on default COS, then enable Zimlet");
/* 1828 */     System.out.println("\tundeploy {zimlet} - remove the Zimlet from the system");
/* 1829 */     System.out.println("\tinstall {zimlet.zip} - installs the Zimlet files on this host");
/* 1830 */     System.out.println("\tldapDeploy {zimlet} - add the Zimlet entry to the system");
/* 1831 */     System.out.println("\tenable {zimlet} - enables the Zimlet");
/* 1832 */     System.out.println("\tdisable {zimlet} - disables the Zimlet");
/* 1833 */     System.out.println("\tacl {zimlet} {cos1} grant|deny [{cos2} grant|deny...] - change the ACL for the Zimlet on a COS");
/*      */     
/* 1835 */     System.out.println("\tlistAcls {zimlet} - list ACLs for the Zimlet");
/* 1836 */     System.out.println("\tlistZimlets - show status of all the Zimlets in the system.");
/* 1837 */     System.out.println("\tgetConfigTemplate {zimlet.zip} - dumps the configuration");
/* 1838 */     System.out.println("\tconfigure {config.xml} - installs the configuration");
/* 1839 */     System.out.println("\tlistPriority - show the current Zimlet priorities (0 high, 9 low)");
/* 1840 */     System.out.println("\tsetPriority {zimlet} {priority} - set Zimlet priority");
/* 1841 */     System.out.println("\tinfo {zimlet} - show information about zimlet");
/* 1842 */     System.out.println("\tcreateZip {zimlet directory} [description-file] - creates zimlet.zip from the contents in the directory");
/*      */     
/* 1844 */     System.exit(1);
/*      */   }
/*      */   
/*      */   private static int lookupCmd(String cmd) {
/* 1848 */     Integer i = (Integer)mCommands.get(cmd.toLowerCase());
/* 1849 */     if (i == null) {
/* 1850 */       usage();
/*      */     }
/* 1852 */     return i.intValue();
/*      */   }
/*      */   
/*      */   private static void dispatch(String[] args) {
/* 1856 */     boolean isLocalInstall = false;
/*      */     try {
/* 1858 */       if (args[argPos].equals("-l")) {
/* 1859 */         isLocalInstall = true;
/* 1860 */         argPos += 1;
/*      */       }
/* 1862 */       String adminURL = null;
/* 1863 */       String uploadURL = null;
/* 1864 */       if (args[argPos].equals("-a")) {
/* 1865 */         adminURL = args[(++argPos)];
/* 1866 */         argPos += 1;
/*      */       }
/* 1868 */       if (args[argPos].equals("-u")) {
/* 1869 */         uploadURL = args[(++argPos)];
/* 1870 */         argPos += 1;
/*      */       }
/* 1872 */       if (argPos >= args.length) {
/* 1873 */         usage();
/*      */       }
/*      */       
/* 1876 */       int cmd = lookupCmd(args[(argPos++)]);
/* 1877 */       switch (cmd) {
/*      */       case 12: 
/* 1879 */         boolean everything = false;
/* 1880 */         if ((args.length > argPos) && (args[argPos].equals("all"))) {
/* 1881 */           everything = true;
/*      */         }
/* 1883 */         listAllZimlets(everything);
/* 1884 */         System.exit(0);
/*      */       case 21: 
/* 1886 */         listPriority();
/* 1887 */         System.exit(0);
/*      */       case 99: 
/* 1889 */         test();
/* 1890 */         System.exit(0);
/*      */       }
/*      */       
/* 1893 */       if (args.length < argPos + 1) {
/* 1894 */         usage();
/*      */       }
/* 1896 */       String zimlet = args[(argPos++)];
/* 1897 */       switch (cmd) {
/*      */       case 18: 
/* 1899 */         deployZimletLocally(new ZimletFile(zimlet));
/* 1900 */         if (!isLocalInstall) {
/* 1901 */           boolean synchronous = false;
/* 1902 */           if ((args.length > argPos) && (args[argPos].equals("sync"))) {
/* 1903 */             synchronous = true;
/*      */           }
/* 1905 */           deployZimletRemotely(zimlet, adminURL, uploadURL, synchronous); }
/* 1906 */         break;
/*      */       
/*      */       case 10: 
/* 1909 */         installZimletLocally(new ZimletFile(zimlet));
/* 1910 */         break;
/*      */       case 11: 
/* 1912 */         undeployZimletLocally(zimlet);
/* 1913 */         if (!isLocalInstall) {
/* 1914 */           undeployZimletRemotely(zimlet);
/*      */         }
/*      */         break;
/*      */       case 17: 
/* 1918 */         ldapDeploy(zimlet);
/* 1919 */         break;
/*      */       case 13: 
/* 1921 */         if ((args.length < argPos + 2) || (args.length % 2 != 0)) {
/* 1922 */           usage();
/*      */         }
/* 1924 */         aclZimlet(zimlet, args);
/* 1925 */         break;
/*      */       case 22: 
/* 1927 */         if (args.length < argPos + 1) {
/* 1928 */           usage();
/*      */         }
/* 1930 */         setPriority(zimlet, Integer.parseInt(args[argPos]));
/* 1931 */         listPriority();
/* 1932 */         break;
/*      */       case 14: 
/* 1934 */         listAcls(zimlet);
/* 1935 */         break;
/*      */       case 19: 
/* 1937 */         enableZimlet(zimlet);
/* 1938 */         break;
/*      */       case 20: 
/* 1940 */         disableZimlet(zimlet);
/* 1941 */         break;
/*      */       case 15: 
/* 1943 */         dumpConfig(zimlet);
/* 1944 */         break;
/*      */       case 16: 
/* 1946 */         installConfig(new String(ByteUtil.getContent(new File(zimlet))));
/* 1947 */         break;
/*      */       case 23: 
/* 1949 */         showInfo(zimlet);
/* 1950 */         break;
/*      */       case 24: 
/* 1952 */         createZip(zimlet, args.length == argPos ? null : args[argPos]);
/* 1953 */         break;
/*      */       case 12: case 21: default: 
/* 1955 */         usage();
/*      */       }
/*      */     }
/*      */     catch (ArrayIndexOutOfBoundsException ex) {
/* 1959 */       usage();
/*      */     } catch (Exception e) {
/* 1961 */       e.printStackTrace();
/* 1962 */       if (sQuietMode) {
/* 1963 */         ZimbraLog.zimlet.error("Error " + e.getMessage());
/*      */       } else {
/* 1965 */         ZimbraLog.zimlet.error("Error", e);
/*      */       }
/* 1967 */       System.exit(1);
/*      */     }
/*      */   }
/*      */   
/* 1971 */   private static boolean sQuietMode = false;
/* 1972 */   private static int argPos = 0;
/*      */   
/*      */   private static void getOpt(String[] args) {
/* 1975 */     if (args.length < 1) {
/* 1976 */       usage();
/*      */     }
/*      */     
/* 1979 */     int index = 0;
/* 1980 */     while ((index < args.length) && 
/* 1981 */       (args[index].equals("-q"))) {
/* 1982 */       sQuietMode = true;
/*      */       
/*      */ 
/*      */ 
/* 1986 */       index++;
/*      */     }
/* 1988 */     argPos = index;
/*      */   }
/*      */   
/*      */   public static void main(String[] args) throws IOException
/*      */   {
/* 1993 */     getOpt(args);
/* 1994 */     if (sQuietMode) {
/* 1995 */       CliUtil.toolSetup("WARN");
/*      */     } else {
/* 1997 */       CliUtil.toolSetup();
/*      */     }
/* 1999 */     SoapTransport.setDefaultUserAgent("zmzimletctl", BuildInfo.VERSION);
/* 2000 */     setup();
/* 2001 */     dispatch(args);
/*      */   }
/*      */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/zimlet/ZimletUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */